package com.kcl.dfss.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class AjustView extends ImageView {

	private Bitmap mBackgroundBitmap = null;
	private Paint mPaint = null;
	private float mXstart = 0.0f;
	private float mYstart = 0.0f;
	private int mWidth = 0;
	private int mHeight = 0;
	private int mPointIndex = 0;
	private float startX = 0;
	private float startY = 0;
	private boolean mMoveVertical = false;
	private boolean mMoveHorizontal = false;
	private float mTans = 0.364f;// tan20
	private boolean minit = true;
	private String TAG = "AjustView";

	public AjustView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public AjustView(Context context, AttributeSet attr) {
		super(context, attr);
		init();
	}

	public void setImageBitmap(Bitmap bitmap) {
		Log.d(TAG, "set bitmap");
		mBackgroundBitmap = bitmap;
		invalidate();
	}

	private void init() {
		mPaint = new Paint();

	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (minit) {
			mWidth = getMeasuredWidthAndState();
			mHeight = getMeasuredHeightAndState();
			mXstart = mWidth / 2.0f;
			mYstart = mHeight / 2.0f;
			minit = false;
		}

		if (mBackgroundBitmap != null) {
			canvas.drawBitmap(mBackgroundBitmap, new Matrix(), mPaint);
		}

		mPaint.setStyle(Style.STROKE);// 设置非填充
		mPaint.setStrokeWidth(5);// 笔宽5像素
		mPaint.setColor(Color.RED);// 设置为红笔
		mPaint.setAntiAlias(true);// 锯齿不显示

		canvas.drawLine(0, mYstart, mWidth, mYstart, mPaint);

		mPaint.setColor(Color.DKGRAY);
		canvas.drawLine(mXstart, 0, mXstart, mHeight, mPaint);
	}

	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mPointIndex = 0;
			startX = event.getX();
			startY = event.getY();
			mMoveVertical = false;
			mMoveHorizontal = false;
			break;
		case MotionEvent.ACTION_MOVE:
			mPointIndex++;
			if (mPointIndex == 2) {
				float Ytan = (startX - event.getX()) / (startY - event.getY());
				Log.d(TAG, "Ytan is " + Ytan);
				if ((Ytan > -mTans && Ytan < 0) || (Ytan < mTans && Ytan > 0)) {
					mMoveVertical = true;
				} else {
					float Xtan = 1 / Ytan;
					if ((Xtan > -mTans && Xtan < 0)
							|| (Xtan < mTans && Xtan > 0)) {
						mMoveHorizontal = true;
					}
				}
			}
			if (mMoveHorizontal) {
				Log.d(TAG, "move horizontal");
				mXstart = event.getX();
				if (mXstart > mWidth) {
					mXstart = mWidth;
				} else if (mXstart < 0) {
					mXstart = 0;
				}
				invalidate();
			}
			if (mMoveVertical) {
				Log.d(TAG, "move vertical");
				mYstart = event.getY();
				if (mYstart > mHeight) {
					mYstart = mHeight;
				} else if (mYstart < 0) {
					mYstart = 0;
				}
				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			Log.d(TAG, "mXstart and mYstart are " + mXstart + " " + mYstart);
			Log.d(TAG, "mWidth and mHeight are " + mWidth + "  " + mHeight);
			break;
		default:
			break;
		}
		return true;
	}

	public int[] getAjustResult() {
		int[] array = new int[2];
		array[0] = (int) (mXstart / mWidth * 640);
		array[1] = (int) (mYstart / mHeight * 480);
		return array;
	}
}
